#include "ffz/raft/raft-log.h"
#define MAX_RAFT_LOG 100
// only save 39 elements, because raftLogs[0] will not be used
RaftLog raftLogs[40];
uint16_t lastLogIndex = 0;
uint16_t commitIndex = 0;
uint16_t getLastLogIndex()
{
    return lastLogIndex;
}
RaftLog *getRaftLog(uint16_t index)
{
    return &raftLogs[index];
}
void setRaftLog(RaftLog *log)
{
    raftLogs[log->index].index = log->index;
    raftLogs[log->index].action = log->action;
    raftLogs[log->index].term = log->term;
    raftLogs[log->index].status = log->status;
}
void setRaftLogLastIndex(uint16_t index)
{
    lastLogIndex = index;
}
void addRaftLog(uint16_t term, uint8_t status, uint8_t action)
{
    lastLogIndex++;
    raftLogs[lastLogIndex].term = term;
    raftLogs[lastLogIndex].status = status;
    raftLogs[lastLogIndex].action = action;
    raftLogs[lastLogIndex].index = lastLogIndex;
}

//Some tool functions
void getLastLogIndexAndTerm(uint16_t *lastLogIndex, uint16_t *lastLogTerm)
{
    RaftLog *lastLog = getRaftLog(getLastLogIndex());
    *lastLogIndex = lastLog->index;
    *lastLogTerm = lastLog->term;
}

void setCommitIndex(uint16_t index)
{
    commitIndex = index;
}
uint8_t getCommitIndex()
{
    return commitIndex;
}